//
//  YBMyWalletTableViewCell.m
//  Nectar
//
//  Created by famer on 2023/11/8.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBMyWalletTableViewCell.h"
#import "UIImageView+WebCache.h"

@implementation YBMyWalletTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        _logoImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_logoImageView];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor hexColor:@"1E1E1E"];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.numberOfLines = 0;
        _nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentView addSubview:_nameLabel];
        
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.font = [UIFont systemFontOfSize:12];
        _addressLabel.textColor = [UIColor hexColor:@"84828C"];
        _addressLabel.textAlignment = NSTextAlignmentLeft;
        _addressLabel.numberOfLines = 0;
        _addressLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentView addSubview:_addressLabel];
        
        _moreImageView = [[UIImageView alloc] init];
        [_moreImageView setImage:[UIImage imageNamed:@"icon_home_more_black"]];
        [self.contentView addSubview:_moreImageView];
        
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.backgroundColor = kClearColor;
        [_moreButton setTitle:@"" forState:UIControlStateNormal];
        [_moreButton setTitle:@"" forState:UIControlStateSelected];
        [_moreButton addTarget:self action:@selector(onMoreClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_moreButton];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kThemeSplitLineColor;
        [self.contentView addSubview:_lineView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.size.width;
    CGFloat height = self.contentView.size.height;
    
    self.logoImageView.frame = CGRectMake(0, 0, 30, 30);
    self.logoImageView.centerY = height*0.5;
    
    self.moreImageView.frame = CGRectMake(width-24, 0, 24, 24);
    self.moreImageView.centerY = height*0.5;
    
    self.moreButton.frame = CGRectMake(width-24, 0, 24, 40);
    self.moreButton.centerY = height*0.5;
    
    CGFloat addressLabelWidth = self.moreImageView.left-(self.logoImageView.right+10);
    CGSize addressLabelSize = [self.addressLabel textBoundingSizeWithMaxSize:CGSizeMake(addressLabelWidth, height)];
    self.addressLabel.frame = CGRectMake(self.logoImageView.right+10, 0, addressLabelWidth, addressLabelSize.height);
    self.addressLabel.centerY = height*0.5+addressLabelSize.height*0.5+5;
    
    CGFloat nameLabelWidth = addressLabelWidth;
    CGSize nameLabelSize = [self.nameLabel textBoundingSizeWithMaxSize:CGSizeMake(nameLabelWidth, height)];
    self.nameLabel.frame = CGRectMake(self.addressLabel.left, 0, nameLabelWidth, nameLabelSize.height);
    self.nameLabel.centerY = height*0.5-nameLabelSize.height*0.5-5;
    
    self.lineView.frame = CGRectMake(0, height-1, width, 1);
}

- (void)setData:(RevDataGetCoins *)item{
    _data = item;
    
    NSString *url = [NSString stringWithFormat:@"%@",item.logo];
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];
    
    self.nameLabel.text = item.coinName;
    self.addressLabel.text = [item.address toEllipsisWalletAddress];
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)onMoreClick:(UIButton *)sender{
    if(self.action){
        self.action(self.data);
    }
}

@end
